
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <SDL/SDL.h>

#define XRES	800
#define YRES	600
//#define SCALE	1
//#define EXTENDED_FONT 1

char width[256];
unsigned char flags[256];
unsigned int color[256];
signed char top[256];
signed char left[256];

void blendpixel(unsigned *vid, int x, int y, int r, int g, int b, int a)
{
	if (x<0 || y<0 || x>=XRES || y>=YRES)
		return;
	if (a != 255)
	{
		int t = vid[y*XRES+x];
		r = (a*r + (255-a)*((t>>16)&255)) >> 8;
		g = (a*g + (255-a)*((t>>8)&255)) >> 8;
		b = (a*b + (255-a)*(t&255)) >> 8;
	}
	vid[y*XRES+x] = (r<<16)|(g<<8)|b;
}

void drawline(unsigned *vid, int x1, int y1, int x2, int y2, int r, int g, int b, int a)
{
	int cp = abs(y2-y1) > abs(x2-x1), x, y, dx, dy, sy;
	float e, de;
	if (cp)
	{
		y = x1;
		x1 = y1;
		y1 = y;
		y = x2;
		x2 = y2;
		y2 = y;
	}
	if (x1 > x2)
	{
		y = x1;
		x1 = x2;
		x2 = y;
		y = y1;
		y1 = y2;
		y2 = y;
	}
	dx = x2 - x1;
	dy = abs(y2 - y1);
	e = 0.0f;
	de = dy/(float)dx;
	y = y1;
	sy = (y1<y2) ? 1 : -1;
	for (x=x1; x<=x2; x++)
	{
		if (cp)
			blendpixel(vid, y, x, r, g, b, a);
		else
			blendpixel(vid, x, y, r, g, b, a);
		e += de;
		if (e >= 0.5f)
		{
			y += sy;
			e -= 1.0f;
		}
	}
}

void drawrect(unsigned *vid, int x, int y, int width, int height, int r, int g, int b, int a)
{
	int x1 = x, x2 = x + width;
	int y1 = y, y2 = y + height;
	for (y=y1 + 1; y<y2; y++)
	{
		blendpixel(vid, x1, y, r, g, b, a);
		blendpixel(vid, x2, y, r, g, b, a);
	}
	x2++;
	for (x=x1; x<x2; x++)
	{
		blendpixel(vid, x, y1, r, g, b, a);
		blendpixel(vid, x, y2, r, g, b, a);
	}
}

void fillrect(unsigned *vid, int x, int y, int width, int height, int r, int g, int b, int a)
{
	int x1 = x, x2 = x + width;
	int y1 = y, y2 = y + height;
	for (x=x1; x<x2; x++)
	{
		for (y=y1; y<y2; y++)
		{
			blendpixel(vid, x, y, r, g, b, a);
		}
	}
}

/***********************************************************
 *                       SDL OUTPUT                        *
 ***********************************************************/

SDL_Surface *sdl_scrn;
int sdl_key;
void sdl_open(void)
{
	if (SDL_Init(SDL_INIT_VIDEO)<0)
	{
		fprintf(stderr, "Initializing SDL: %s\n", SDL_GetError());
		exit(1);
	}
	atexit(SDL_Quit);
	sdl_scrn = SDL_SetVideoMode(XRES*SCALE, YRES*SCALE + 40*SCALE, 32, SDL_SWSURFACE);
	if (!sdl_scrn)
	{
		fprintf(stderr, "Creating window: %s\n", SDL_GetError());
		exit(1);
	}
}

void sdl_blit(int x, int y, int w, int h, unsigned int *src, int pitch)
{
	unsigned *dst,i,j,k;
	if (SDL_MUSTLOCK(sdl_scrn))
		if (SDL_LockSurface(sdl_scrn)<0)
			return;
	dst = (unsigned *)sdl_scrn->pixels+y*sdl_scrn->pitch/4+x;
	for (j = 0;j < h; j++)
	{
		for (k = 0; k < SCALE; k++)
	{
			for (i = 0; i < w*SCALE; i++)
				dst[i] = src[i/SCALE];
			dst += sdl_scrn->pitch/4;
		}
		src += pitch/4;
	}
	if (SDL_MUSTLOCK(sdl_scrn))
		SDL_UnlockSurface(sdl_scrn);
	SDL_UpdateRect(sdl_scrn,0,0,0,0);
}

int frame_idx = 0;
void dump_frame(unsigned int *src, int w, int h, int pitch)
{
	char frame_name[32];
	unsigned j,i,c;
	FILE *f;
	sprintf(frame_name,"frame%04d.ppm",frame_idx);
	f=fopen(frame_name,"w");
	fprintf(f,"P6\n%d %d\n255\n",w,h);
	for (j = 0; j < h; j++)
	{
		for (i = 0; i < w; i++)
		{
			c = ((src[i]&0xFF0000)>>16) | (src[i]&0x00FF00) | ((src[i]&0x0000FF)<<16);
			fwrite(&c,3,1,f);
		}
		src += pitch/4;
	}
	fclose(f);
	frame_idx++;
}

int sdl_poll()
{
	SDL_Event event;
	sdl_key=0;
	while (SDL_PollEvent(&event))
	{
		switch (event.type)
		{
		case SDL_KEYDOWN:
			sdl_key = event.key.keysym.sym;
			break;
		case SDL_QUIT:
			return 1;
		}
	}
	return 0;
}

/***********************************************************
 *                      MAIN PROGRAM                       *
 ***********************************************************/

const char *tag = "Untitled";

int main(int argc, char *argv[])
{
	unsigned *vid_buf = (unsigned*)calloc(XRES*YRES, sizeof(unsigned));
	int x, y, b = 0, lb, c = 0xA0, i, j, dc = 0;
	int mode = 0;
	char hex[18] = "";
	char inputColor = 0;
	FILE *f;

	f = fopen("main.bin", "rb");

	sdl_open();
	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
	
	memset(vid_buf, 0, XRES*YRES*4);
	fillrect(vid_buf, 200, 200, 200, 200, 255, 255,   0, 255);
	drawline(vid_buf, 150,  50, 300, 350, 255,   0, 255, 255);
	drawrect(vid_buf, 300, 300, 200, 150,   0, 255,   0, 255);
	sdl_blit(0, 0, XRES, YRES, vid_buf, XRES*4);
		
	while (!sdl_poll())
	{
		
	}
}
